<?php
function deperb_add($PERB_NO, $DETACHIEV_ID, $DEPERB_TEMPAT, $DEPERB_TGL, $DEPERB_WAKTU, $DEPERB_COUNT, $DEPERB_STATUS, $DEPERB_DESC){
	$sql = "INSERT INTO detil_perbuatan 
			VALUES ('', '$PERB_NO',	'$DETACHIEV_ID', '$DEPERB_TEMPAT', '$DEPERB_TGL', 
					'$DEPERB_WAKTU', $DEPERB_COUNT, $DEPERB_STATUS, '$DEPERB_DESC')";
	return mysql_query($sql);
}

function deperb_select($key=false, $where=false, $orderby=false, $groupby=false){
	$sql = "SELECT * FROM detil_perbuatan as a ". ($key||$where? " WHERE " : "");
	if ($key){
		$sql .= "(
				a.DEPERB_NO		LIKE '%$key%' OR				
				a.PERB_NO		LIKE '%$key%' OR				
				a.DETACHIEV_ID 	LIKE '%$key%' OR				
				a.DEPERB_TEMPAT LIKE '%$key%' OR				
				a.DEPERB_TGL 	LIKE '%$key%' OR				
				a.DEPERB_WAKTU 	LIKE '%$key%' OR				
				a.DEPERB_COUNT 	LIKE '%$key%' OR				
				a.DEPERB_STATUS LIKE '%$key%')";
	}
	if ($where) $sql .= $key? " AND $where " : " $where ";
	if ($groupby) $sql .= " GROUP BY $groupby ";
	if ($orderby) $sql .= " GROUP BY $orderby ";
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function deperb_countKegiatan($karyawanPelaku, $DETACHIEV_ID){
	$sql = "SELECT DISTINCT COUNT(b.DEPERB_NO) as COUNT 
			FROM perbuatan as a, detil_perbuatan as b 
			WHERE a.PERB_NO=b.PERB_NO AND 
				a.KODE_KARYAWAN='$karyawanPelaku' AND 
				b.DETACHIEV_ID='$DETACHIEV_ID' AND 
				b.DEPERB_STATUS='2'";
	$data = mysql_query($sql);
	$data = mysql_fetch_assoc($data);
	return intval($data['COUNT']);
}

function deperb_load($deperbNO){
	$sql = "SELECT * FROM detil_perbuatan WHERE DEPERB_NO='$deperbNO'";
	return mysql_query($sql);
}

function deperb_load_last(){
	$sql = "SELECT MAX(DEPERB_NO) as DEPERB_NO FROM detil_perbuatan";
	return mysql_query($sql);
}

function deperb_setStatus($deperbNO, $status){
	$sql= "UPDATE detil_perbuatan 
			SET DEPERB_STATUS='$status'
			WHERE DEPERB_NO='$deperbNO'";
	return mysql_query($sql);
}

function deperb_setCount($deperbNO, $count){
	$sql= "UPDATE detil_perbuatan 
			SET DEPERB_COUNT='$count'
			WHERE DEPERB_NO='$deperbNO'";
	return mysql_query($sql);
}

function deperb_delete($deperbNO=false, $perbNO=false){
	$sql = "DELETE FROM detil_perbuatan " . ($deperbNO || $perbNO? " WHERE " : "");
	if ($deperbNO){	$sql .= " DEPERB_NO='$deperbNO' "; }
	if ($perbNO){	$sql .= ($deperbNO?" AND ":"") ." PERB_NO='$perbNO' "; }
	return mysql_query($sql);
}